package sskj.regulatory.presentation.screen.qrlogin

import android.content.Intent
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import kotlinx.android.synthetic.main.fragment_qrlogin.view.*
import sskj.regulatory.R
import sskj.regulatory.presentation.base_mvp.base.BaseFragment
import sskj.regulatory.presentation.screen.home.HomeFragment
import sskj.regulatory.presentation.screen.login.LoginFragment
import sskj.regulatory.presentation.screen.main.MainActivity
import sskj.regulatory.presentation.screen.main.MainFragment
import sskj.regulatory.presentation.utils.delegates.bundle
import javax.inject.Inject

class QrLoginFragment : BaseFragment<QrLoginContract.View, QrLoginContract.Presenter>(), QrLoginContract.View {

    @Inject
    lateinit var mPresenter: QrLoginPresenter

    override fun injectDependencies() = activityComponent.inject(this)

    override fun onBackPressed(): Boolean = true

    override fun initPresenter() = mPresenter

    override val layoutResId: Int = R.layout.fragment_qrlogin

    override fun onFetchTransitionConfig(): TransitionConfig {
        return POP_TRANSITION_CONFIG
    }

    override fun initView() {
        initTopBar()
        initEvent()
        mCacheView.qrLoginTv.text = "即将登陆${when (arguments?.getString("type").toString()) {
            "XMB" -> "项目部系统"
            else -> ""
        }}windows网页端"
    }

    private fun initEvent() {
        mCacheView.btnLogin.setOnClickListener { mPresenter.qrLogin(arguments?.getString("qrCode").toString()) }
    }

    private fun initTopBar() {
        mCacheView.qrTopBar.setTitle("扫码登录")
        mCacheView.qrTopBar.addLeftTextButton("取消", R.id.topbar_left_cancel_button).setOnClickListener { popBackStack() }
    }

    override fun loginOk() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_SUCCESS)
                .setTipWord("登录成功")
                .create()
        activity.tipDialog?.show()

        view?.postDelayed({

            baseFragmentActivity.popBackStack(MainFragment::class.java)
            activity.tipDialog?.dismiss()

        }, 1500)
    }

    override fun showLoading() {
        activity.tipDialog = QMUITipDialog.Builder(context)
                .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                .setTipWord("正在加载")
                .create()
        activity.tipDialog?.show()
    }

    override fun hideLoading() {
        activity.tipDialog?.dismiss()
    }

    override fun showError(message: String?) {
        QMUIDialog.MessageDialogBuilder(getActivity())
                .setTitle("错误提示")
                .setMessage(message)
                .addAction("确定") { dialog, _ -> dialog.dismiss() }
                .create(R.style.QMUI_Dialog).show()
    }
}